package edu.uwm.universitydb.controller;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;

import edu.uwm.universitydb.service.ServiceFactory;
import edu.uwm.universitydb.service.dept.DepartmentService;
import edu.uwm.universitydb.swing.DepartmentTableModel;
import edu.uwm.universitydb.view.DepartmentMaint;

public class DepartmentMaintController implements MouseListener {
	
	public DepartmentMaintController() {
		_departmentService = ServiceFactory.getInstance().getDepartmentService();
		// Create our associated view
		_view = new DepartmentMaint(new DepartmentTableModel(_departmentService));
		
		// Setup our listeners
		//_view.getAddButton().addMouseListener(this);
		//_view.getDelButton().addMouseListener(this);

	}
	
	private void onAddButtonClick() {
		//Dummy values
		String department= "";
		
		//Show add Department View
		JPanel panel = _view.showNewView();
		boolean validated = false;
		while (!validated) {
			int result = JOptionPane.showConfirmDialog(_view, panel, "Add", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
			if (result == JOptionPane.OK_OPTION) {
				//Get user inputs
				for( Component c : panel.getComponents()) {
					if (DepartmentMaint.DEPARTMENT.equals(c.getName())) {
						department = ((JTextField)c).getText();
					}
				}
				// Add User
				boolean status = true; //_departmentService.createDepartment(new Department(department));
				if (status == false) {
					JOptionPane.showMessageDialog(_view, "Error adding department!");
				} else {
					validated = true;
				}
			} else {
				//User cancelled add
				break;
			}
			_view.refreshTable();
		}
	}
	
	private void onDelButtonClick() {
		JTable dataTable = _view.getDataTable();
		int numRows = dataTable.getSelectedRowCount();
		
		//At least 1 row needs to be selected
		if (numRows == 0) {
			JOptionPane.showMessageDialog(_view, "Please select row/s to Delete!");
		} else {
			//Confirm delete
			int confirm = JOptionPane.showConfirmDialog(_view,
					"Are you sure you want to delete?", "Confirm", JOptionPane.YES_NO_OPTION);
			if (confirm == JOptionPane.YES_OPTION) {
				List<String> selectedKeys = new ArrayList<String>();
				boolean status = true;
				for (int i : dataTable.getSelectedRows()) {
					String keyVal = (String)dataTable.getValueAt(i, 0);
					selectedKeys.add(keyVal);
				}
				//Perform Delete
				for (String key : selectedKeys) {
					status = true; //_departmentService.deleteDepartment(key);
					if (status == false) {
						JOptionPane.showMessageDialog(_view, "Internal error deleting department!");
						break;
					}
				}
				dataTable.clearSelection();
				_view.repaint();
			}
		}
	}
	
	public DepartmentMaint getView() {
		return _view;
	}
	
	
	@Override
	public void mouseClicked(MouseEvent e) {
		//Based on the element clicked, fire off appropriate handler method.
		switch(e.getComponent().getName()) {
			/*case UserMaint.EDITBUTTON: 
				this.onEditButtonClick();
				break;*/
			case DepartmentMaint.DELBUTTON: 
				this.onDelButtonClick();
				break;
			case DepartmentMaint.ADDBUTTON: 
				this.onAddButtonClick();
				break;
			default: 
				//Not sure what they clicked, do nothing
				break;
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// Unused		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// Unused
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// Unused
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// Unused
	}	
	

	private DepartmentMaint _view;
	
	private DepartmentService _departmentService;
	
	public static final String ID = "DepartmentMnt";
}
